package com.koron.car.web.maintain.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.car.web.maintain.bean.query.CarMaintainQuery;
import com.koron.car.web.maintain.bean.vo.CarMaintainVO;
import com.koron.car.web.maintain.filter.MaintainFilter;
import com.koron.car.web.maintain.service.CarMaintainService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/car/maintain")
@Api(tags = "车辆管理-维修保养")
public class CarMaintainController implements WorkflowController {
    /**
     * 服务对象
     */
    @Autowired
    private CarMaintainService maintainService;

    @Autowired
    private MaintainFilter filter;

    @GetMapping("/selectOne")
    @ApiOperation("根据维修保养id查询信息")
    @ApiImplicitParam(name = "maintainId", value = "维修保养id", required = true)
    public Response<CarMaintainVO> selectOne(String maintainId) {
        log.info("根据维修保养id查询信息， maintainId = {}", maintainId);
        CarMaintainVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "queryById", CarMaintainVO.class, maintainId);
        return Response.ok(bean);
    }

    @GetMapping("/queryList")
    @ApiOperation("根据条件查询维修保养列表")
    public Response<PageInfo<CarMaintainVO>> queryList(CarMaintainQuery query) {
        log.info("根据条件查询维修保养列表， query = {}", JSON.toJSONString(query));
        query.getOrders().forEach(orderItem -> {
            if (orderItem.getColumn().contains("describe")) {
                orderItem.setColumn("`describe`");
            }
        });
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<CarMaintainVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("创建/修改维修保养")
    public Response<CarMaintainVO> saveOrUpdate(@RequestBody CarMaintainVO maintain) {
        log.info("创建/修改维修保养， maintain = {}", JSON.toJSONString(maintain));
        CarMaintainVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, StringUtils.isBlank(maintain.getId()) ? "insert" : "update", CarMaintainVO.class, maintain);
        return Response.ok(bean);
    }

    @GetMapping("/submit")
    @ApiOperation("提交维修保养")
    @ApiImplicitParam(name = "maintainId", value = "维修保养id", required = true)
    public Response<CarMaintainVO> submit(String maintainId) {
        log.info("提交维修保养， maintainId = {}", maintainId);
        CarMaintainVO maintain = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "submit", CarMaintainVO.class, maintainId);
        return Response.ok(maintain);
    }

    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流更新： {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流完结 ： {}", JSON.toJSONString(redirectQuery));
        CarMaintainVO bean = JSON.parseObject(redirectQuery.getFormVariables(), CarMaintainVO.class);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "auditFinish", CarMaintainVO.class, bean);
        return Response.success();
    }

    @Override
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateCancel = {}", JSON.toJSONString(redirectQuery));
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("用车申请审批设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("用车申请审批选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }

    @GetMapping("/queryRepairPlace")
    public Response<List<String>> queryRepairPlace(){
        List<String> repairPlaceList = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "queryRepairPlace", List.class);
        return Response.ok(repairPlaceList);
    }

    @GetMapping("/getPrintData")
    @ApiOperation("获取维修申请打印记录")
    @ApiImplicitParams(
            {
            @ApiImplicitParam(name = "findId", value = "流程data_id", required = true),
            @ApiImplicitParam(name = "id", value = "维修保养id", required = true)
            }
    )
    public Response<Map> getPrintData(@RequestParam(required = false) String findId, @RequestParam(required = false) String id){
        if (Objects.isNull(findId) || Objects.isNull(id)){
            return Response.ok();
        }
        Map<String,Object> result = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), maintainService, "getPrintData", Map.class, findId, id);
        return Response.ok(result);
    }

}
